package com.example;

import java.io.IOException;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * projectName rxretrofit
 * date 2017/7/7
 * description com.example
 */

public class RxExceptionSample {
    private static final String TAG = "RxExceptionSample";

    public static void main(String[] args) {
        test4();
    }

    static Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> subscriber) {
            for (int i = 0; i < 5; i++) {
                if (i == 3) {
                    throw new RuntimeException("runtime error");
//                    subscriber.onError(new Throwable("ERROR"));
                } else {
                    subscriber.onNext(i + "");
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {

                }
            }
            subscriber.onCompleted();
        }
    });

    static Subscriber<String> mySubscriber = new Subscriber<String>() {
        @Override
        public void onNext(String s) {
            System.out.println("onNext................." + s);
        }

        @Override
        public void onCompleted() {
            System.out.println("onCompleted.................");
        }

        @Override
        public void onError(Throwable throwable) {
            System.out.println("onError....................."+throwable.getMessage());
        }
    };

    public static void test1() {
        observable.onErrorReturn(new Func1<Throwable, String>() {
                    @Override
                    public String call(Throwable throwable) {
                        return "this is an error observable";
                    }
                })
                .subscribe(mySubscriber);
    }

    public static void test2(){
//        observable
//                .onErrorResumeNext(Observable.just("this is an error observable","error2"))
//                .subscribe(mySubscriber);

        observable.onErrorResumeNext(new Func1<Throwable, Observable<? extends String>>() {
            @Override
            public Observable<? extends String> call(Throwable throwable) {
                return Observable.just(throwable.getMessage());
            }
        }).subscribe(mySubscriber);
    }

    public static void test3(){
        observable.onExceptionResumeNext(Observable.just("aaaaaaaaaaaaaa"))
                .subscribe(mySubscriber);
    }

    public static void test4(){
        observable.retry(1)
                .subscribe(mySubscriber);
    }

    public static void test5(){
        observable.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
            @Override
            public Observable<?> call(final Observable<? extends Throwable> observable) {
                return observable.flatMap(new Func1<Throwable, Observable<?>>() {
                    @Override
                    public Observable<?> call(Throwable throwable) {
                        if(throwable instanceof RuntimeException){
                            return Observable.just("zhuyingming");
                        }else{
                            return Observable.error(throwable);
                        }
                    }
                });
            }
        }).subscribe(mySubscriber);
    }
}
